package com.heima.common.tess4j;

import lombok.Getter;
import lombok.Setter;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.awt.image.BufferedImage;

@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "tees4j")
public class Tess4jClient {
	private String dataPath;
	private String languge;

	public String doOCR(BufferedImage image) throws TesseractException {
		//创建tesseract对象
		Tesseract tesseract = new Tesseract();
		//设置字体库路径
		tesseract.setDatapath(dataPath);
		//中文识别
		tesseract.setLanguage(languge);
		//执行OCR识别
		String doOCR = tesseract.doOCR(image);
		//替换回车和tab键 使得结果为一行
		doOCR = doOCR.replaceAll("\\r|\\n", "-").replaceAll(" ", "");
		return doOCR;
	}
}
